package com.glsc.ngateway.platform.controller.cmdb;

import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbMachineCabinet;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.service.cmdb.CmdbMachineCabinetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.glsc.ngateway.common.api.common.enums.DictConstant.CMDB_MACHINE_CABINET;

@RestController
@Api(value = "机柜管理", tags = {"机柜管理"})
public class CmdbMachineCabinetController {

    @Autowired
    private CmdbMachineCabinetService cmdbMachineCabinetService;
    @Autowired
    private UserCache userCache;
    @Autowired
    private HttpServletRequest request;

    /**
     * 查询
     * @param params
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/machineCabinet/search")
    @ApiOperation(value = "查询", notes = "查询")
    public PlatformResponse<Object> searchMachineCabinet(@RequestBody Object params) {
        Object result = cmdbMachineCabinetService.search(CMDB_MACHINE_CABINET,params);
        return PlatformResponse.successData(result);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/machineCabinet/addOrUpdate")
    @ApiOperation(value = "修改机柜", notes = "修改机柜")
    public PlatformResponse<Void> addOrUpdateMachineCabinet(@RequestBody CmdbMachineCabinet cmdbMachineCabinet) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbMachineCabinetService.addOrUpdateMachineCabinet(cmdbMachineCabinet,account);
        return PlatformResponse.successMsg("处理成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "/machineCabinet/delete")
    @ApiOperation(value = "删除机柜", notes = "删除机柜")
    public PlatformResponse<Void> deleteMachineCabinet(@RequestBody CmdbMachineCabinet cmdbMachineCabinet) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbMachineCabinetService.deleteMachineCabinet(cmdbMachineCabinet,account);
        return PlatformResponse.successMsg("删除成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "/machineCabinet/deleteList")
    @ApiOperation(value = "批量删除机柜", notes = "批量删除机柜")
    public PlatformResponse<Void> deleteListMachineCabinet(@RequestBody List<String> codes) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbMachineCabinetService.deleteByCodeList(codes,account);
        return PlatformResponse.successMsg("删除成功");
    }
}

